
package com.unicom.priceboard.data.hose;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;

import com.unicom.priceboard.data.vo.PutExecElement;
import com.unicom.priceboard.thread.PutExecThread;
import com.unicom.priceboard.thread.SecurityThread;
import com.unicom.priceboard.util.NumberUtil;
import com.unicom.priceboard.util.Util;

/**
 * Creation date: Jul 16, 2008
 * @author quan
 */
public class PutExecReader {
	private static final Logger logger = Logger.getLogger(PutExecReader.class);
	
	public void parseExec(String path){
		try{
			FileInputStream instrPutExec = new FileInputStream(Util.getCurrentDataPath(path));
			parsePutExec(instrPutExec);
			instrPutExec.close();
		}catch(Exception e)	{
			logger.debug("not read file: "+e.getMessage());
		}
	}
	
	private void parsePutExec(InputStream is) {
		//List<PutExecElement> records = new ArrayList<PutExecElement>();
		DataInputStream input = new DataInputStream(is);
		try {
			while (input.available()>0) {
				PutExecElement record = new PutExecElement();
				// Create a new ByteBuffer to store a record from file
				ByteBuffer buf = ByteBuffer.allocate(15);
				// Set to little endian
				buf.order(ByteOrder.LITTLE_ENDIAN);
				// Get each field from the file and put it to the byte buffer
				buf.putInt(input.readInt()); // ConfirmNo
				buf.putShort(input.readShort()); // SecurityNo
				buf.putInt(input.readInt()); // Volume
				buf.putInt(input.readInt()); // Price
				buf.put(input.readByte()); // Board

				// Reset position for reading
				buf.flip();
				// Set to big endian
				buf.order(ByteOrder.BIG_ENDIAN);

				// Retrieve data from byte buffer
				record.setConfirmNo(buf.getInt());
				record.setSecurityNo(buf.getShort());
				record.setVolume(String.valueOf(buf.getInt()));
				record.setPrice(String.valueOf((double)buf.getInt()/100));
				record.setBoard((char) buf.get());
				//format price and volume
				String volume=String.valueOf(record.getVolume());
				String price=String.valueOf(record.getPrice());
				volume=NumberUtil.formatNumberPtd(volume);
				price =NumberUtil.formatNumber(price);
				record.setPrice(price);
				record.setVolume(volume);
				
				String symbol=SecurityThread.lstSymbols.get(record.getSecurityNo());
				
				  record.setSymbolStock(symbol);
				if(symbol!=null){
					if(!PutExecThread.mapPutExec.containsKey(record.getConfirmNo())) {
						PutExecThread.mapPutExec.put(String.valueOf(record.getConfirmNo()),record);
					}else {
						PutExecElement objInList = (PutExecElement)PutExecThread.mapPutExec.get(record.getConfirmNo());
						if(!objInList.equals(record)) {
							PutExecThread.mapPutExec.put(String.valueOf(record.getConfirmNo()),record);
						}
					}
				}//end symbol null
			}
			//Regenerating string list
			String strTempConfirmNo = generatePutExec();
			//Update to client if there's any change
			if(!PutExecThread.strConfirmNo.equals(strTempConfirmNo)) {
				PutExecThread.strConfirmNo = strTempConfirmNo;//replace strConfirmNo with new value
				
			}
		} catch (Exception ex) {
			}
	}
	//general putExec
	public synchronized static String generatePutExec()	{
		StringBuffer retString = new StringBuffer("");
		List<String> sortList = new ArrayList<String>();
		
		sortList.addAll(PutExecThread.mapPutExec.keySet());
		Collections.sort(sortList);
		
		for(int i=0;i<sortList.size();i++) {
			if(i<sortList.size()-1)
				retString.append(sortList.get(i)+",");
			else
				retString.append(sortList.get(i));
		}
		
		return retString.toString();
	}
}
